/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/

using Votations.NSurvey.WebAdmin;

namespace Votations.NSurvey.WebAdmin.UserControls
{
	using System;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using Votations.NSurvey;
	using Votations.NSurvey.Data;
	using Votations.NSurvey.DataAccess;
	using Votations.NSurvey.BusinessRules;
	using Votations.NSurvey.Enums;
	using Microsoft.VisualBasic;
	using Votations.NSurvey.Web;
	using Votations.NSurvey.WebControlsFactories;
	using Votations.NSurvey.WebControls.UI;


	/// <summary>
	/// Survey data CU methods
	/// </summary>
	public class TypeOptionControl : PageBase
	{
		protected System.Web.UI.WebControls.Label MessageLabel;
		protected System.Web.UI.WebControls.Label fieldTypeOptionTitleLabel;
		protected System.Web.UI.WebControls.TextBox TitleTextBox;
		protected System.Web.UI.WebControls.Label XmlFileNameLabel;
		protected System.Web.UI.WebControls.TextBox XmlFileNameTextbox;
		protected System.Web.UI.WebControls.CheckBox SelectionTypeCheckBox;
		protected System.Web.UI.WebControls.CheckBox FieldTypeCheckBox;
		protected System.Web.UI.WebControls.PlaceHolder FieldOptionsPlaceHolder;
		protected System.Web.UI.WebControls.TextBox FieldWidthTextBox;
		protected System.Web.UI.WebControls.TextBox FieldHeightTextBox;
		protected System.Web.UI.WebControls.TextBox FieldMaxLengthTextBox;
		protected System.Web.UI.WebControls.CheckBox FieldShownInResultsCheckBox;
		protected System.Web.UI.WebControls.TextBox JavascriptFunctionNameTextBox;
		protected System.Web.UI.WebControls.TextBox JavascriptErrorMessageTextBox;
		protected System.Web.UI.WebControls.TextBox JavascriptTextBox;
		protected System.Web.UI.WebControls.Button CreateTypeButton;
		protected System.Web.UI.WebControls.Button ApplyChangesButton;
		protected System.Web.UI.WebControls.Literal AllowSelectionLabel;
		protected System.Web.UI.WebControls.Literal FieldEntryLabel;
		protected System.Web.UI.WebControls.Literal FieldWidthLabel;
		protected System.Web.UI.WebControls.Literal FieldHeightLabel;
		protected System.Web.UI.WebControls.Literal FieldLengthLabel;
		protected System.Web.UI.WebControls.Literal ShownInResultsLabel;
		protected System.Web.UI.WebControls.Literal JavascriptFunctionLabel;
		protected System.Web.UI.WebControls.Literal JavascriptErrorMessageLabel;
		protected System.Web.UI.WebControls.Literal JavascriptCodeLabel;
		protected System.Web.UI.WebControls.Literal FieldNameLabel;
		protected System.Web.UI.WebControls.Button MakeBuiltInButton;
		protected System.Web.UI.WebControls.DropDownList DataSourceDropDownList;
		protected System.Web.UI.WebControls.Button DeleteTypeButton;
		protected System.Web.UI.WebControls.Literal DataSourceLabel;
		protected System.Web.UI.WebControls.CheckBox MandatorySelectionCheckBox;
		protected System.Web.UI.WebControls.Literal SelectionRequiredLabel;
		protected System.Web.UI.WebControls.Label SqlQueryLabel;
		protected System.Web.UI.WebControls.TextBox SqlQueryTextbox;
		protected System.Web.UI.WebControls.Label SqlQueryInfoLabel;
		protected PlaceHolder FullOptionPlaceholder;
		public event EventHandler OptionChanged;

		/// <summary>
		/// Id of the answer type to edit
		/// if no id is given put the 
		/// usercontrol in creation mode
		/// </summary>
		public int AnswerTypeId
		{
			get { return (ViewState["AnswerTypeId"]==null) ? -1 : int.Parse(ViewState["AnswerTypeId"].ToString()); }
			set { ViewState["AnswerTypeId"] = value; }
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			MessageLabel.Visible = false;
			LocalizePage();
			
			// Check if any answer type id has been assigned
			if (AnswerTypeId == -1)
			{
				SwitchToCreationMode();
			}
			else
			{
				SwitchToEditionMode();
			}
		}

		private void LocalizePage()
		{
			FieldNameLabel.Text = GetPageResource("FieldNameLabel");
			DataSourceLabel.Text = GetPageResource("DataSourceLabel");
			SelectionRequiredLabel.Text = GetPageResource("SelectionRequiredLabel");
			XmlFileNameLabel.Text = GetPageResource("XmlFileNameLabel");
			AllowSelectionLabel.Text = GetPageResource("AllowSelectionLabel");
			FieldEntryLabel.Text = GetPageResource("FieldEntryLabel");
			FieldWidthLabel.Text = GetPageResource("FieldWidthLabel");
			FieldHeightLabel.Text = GetPageResource("FieldHeightLabel");
			ShownInResultsLabel.Text = GetPageResource("ShownInResultsLabel");
			JavascriptFunctionLabel.Text = GetPageResource("JavascriptFunctionLabel");
			JavascriptErrorMessageLabel.Text = GetPageResource("JavascriptErrorMessageLabel");
			JavascriptCodeLabel.Text = GetPageResource("JavascriptCodeLabel");
			CreateTypeButton.Text = GetPageResource("CreateTypeButton");
			ApplyChangesButton.Text = GetPageResource("ApplyChangesButton");
			DeleteTypeButton.Text = GetPageResource("DeleteTypeButton");
			MakeBuiltInButton.Text = GetPageResource("MakeBuiltInButton");
			SqlQueryLabel.Text = GetPageResource("SqlQueryLabel");
			SqlQueryInfoLabel.Text = GetPageResource("SqlQueryInfoLabel");

			if (!Page.IsPostBack)
			{
				DataSourceDropDownList.Items.Add(new ListItem(GetPageResource("NoneDataSourceOption"), "1"));
				DataSourceDropDownList.Items.Add(new ListItem(GetPageResource("XmlDataSourceOption"), "2"));
				DataSourceDropDownList.Items.Add(new ListItem(GetPageResource("SqlDataSourceOption"), "3"));
			}
		
		}

		private void FieldEntryChecked(object sender, System.EventArgs e)
		{
			InitUserInterface();
		}

		/// <summary>
		/// Setup the control in creation mode
		/// </summary>
		private void SwitchToCreationMode()
		{
			// Creation mode
			fieldTypeOptionTitleLabel.Text = GetPageResource("NewAnswerTypeTitle");;
			CreateTypeButton.Visible = true;
			ApplyChangesButton.Visible = false;
			DeleteTypeButton.Visible = false;
		}

		/// <summary>
		/// Setup the control in edition mode
		/// </summary>
		private void SwitchToEditionMode()
		{
			fieldTypeOptionTitleLabel.Text = GetPageResource("EditAnswerTypeTitle");
			CreateTypeButton.Visible = false;
			ApplyChangesButton.Visible = true;
			DeleteTypeButton.Visible = true;
			MakeBuiltInButton.Visible = false;
		}
		
		/// <summary>
		/// Get the current DB data and fill 
		/// the fields with them
		/// </summary>
		public void BindFields()
		{
			// Retrieve the answer type data
			AnswerTypeData answerTypeData = new AnswerTypes().GetAnswerTypeById(AnswerTypeId);
			AnswerTypeData.AnswerTypesRow answerType = answerTypeData.AnswerTypes[0];
			
			if (answerType.BuiltIn)
			{
				DeleteTypeButton.Attributes.Add("onClick",
					"javascript:if(confirm('" +GetPageResource("DeleteBuiltInTypeConfirmationMessage")+ "')== false) return false;");
				ApplyChangesButton.Attributes.Add("onClick",
					"javascript:if(confirm('" +GetPageResource("ApplyBuiltInChangesConfirmationMessage")+ "')== false) return false;");
			}
			else
			{
				DeleteTypeButton.Attributes.Remove("onClick");
				ApplyChangesButton.Attributes.Remove("onClick");
				MakeBuiltInButton.Attributes.Add("onClick",
					"javascript:if(confirm('" +GetPageResource("MakeBuiltInConfirmationMessage")+ "')== false) return false;");
			}
			

			// Assigns the retrieved data to the correct fields
			TitleTextBox.Text = answerType.Description;
			if (answerType.XmlDataSource != 
				null && answerType.XmlDataSource.Length != 0)
			{
				XmlFileNameTextbox.Text = answerType.XmlDataSource;
				DataSourceDropDownList.SelectedValue = "2";
				
				// Do we have a mandatory xml answer type
				MandatorySelectionCheckBox.Checked = 
					answerType.TypeNameSpace == "Votations.NSurvey.WebControls.UI.AnswerXmlRequiredListItem";
			}
			else if (answerType.DataSource != 
				null && answerType.DataSource.Length != 0)
			{
				SqlQueryTextbox.Text = answerType.DataSource;
				DataSourceDropDownList.SelectedValue = "3";
				// Do we have a mandatory sql answer type
				MandatorySelectionCheckBox.Checked = 
					answerType.TypeNameSpace == "Votations.NSurvey.WebControls.UI.AnswerSqlRequiredListItem";
			}
			else
			{
				DataSourceDropDownList.SelectedValue = "1";
				SelectionTypeCheckBox.Checked = 
					(((AnswerTypeMode)answerType.TypeMode & AnswerTypeMode.Selection) > 0);
				FieldTypeCheckBox.Checked = 
					(((AnswerTypeMode)answerType.TypeMode & AnswerTypeMode.Field) > 0);

				FieldShownInResultsCheckBox.Checked = answerType.PublicFieldResults;
				FieldHeightTextBox.Text = answerType.FieldHeight.ToString();
				FieldWidthTextBox.Text = answerType.FieldWidth.ToString();
				FieldMaxLengthTextBox.Text = answerType.FieldLength.ToString();
				JavascriptFunctionNameTextBox.Text = answerType.JavascriptFunctionName;
				JavascriptTextBox.Text = answerType.JavascriptCode;
				JavascriptErrorMessageTextBox.Text = answerType.JavascriptErrorMessage;
			}

			if ((answerType.BuiltIn && !NSurveyUser.Identity.IsAdmin) || 
				(DataSourceDropDownList.SelectedValue == "3" &&  
				(!GlobalConfig.SqlBasedAnswerTypesAllowed ||
				!CheckRight(NSurveyRights.SqlAnswerTypesEdition, false))))
			{
				ApplyChangesButton.Visible = false;
				DeleteTypeButton.Visible = false;
				MakeBuiltInButton.Visible = false;
				if (DataSourceDropDownList.SelectedValue == "3" 
					&& !CheckRight(NSurveyRights.SqlAnswerTypesEdition, false))
				{
					DataSourceDropDownList.Enabled = false;
				}
				else
				{
					DataSourceDropDownList.Enabled = true;
				}
			}
			else if (answerType.BuiltIn && NSurveyUser.Identity.IsAdmin)
			{
				ApplyChangesButton.Visible = true;
				DeleteTypeButton.Visible = true;
				MakeBuiltInButton.Visible = false;
				DataSourceDropDownList.Enabled = true;
			}
			else
			{
				ApplyChangesButton.Visible = true;
				DeleteTypeButton.Visible = true;
				MakeBuiltInButton.Visible = NSurveyUser.Identity.IsAdmin;
				DataSourceDropDownList.Enabled = true;
			}

			InitUserInterface();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.CreateTypeButton.Click += new System.EventHandler(this.CreateTypeButton_Click);
			this.ApplyChangesButton.Click += new System.EventHandler(this.ApplyChangesButton_Click);
			this.DeleteTypeButton.Click += new System.EventHandler(this.DeleteTypeButton_Click);
			this.MakeBuiltInButton.Click += new System.EventHandler(this.MakeBuiltInButton_Click);
			this.DataSourceDropDownList.SelectedIndexChanged += new System.EventHandler(this.DataSourceDropDownList_SelectedIndexChanged);
			this.FieldTypeCheckBox.CheckedChanged += new System.EventHandler(this.FieldEntryChecked);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void XmlDataSourceCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			InitUserInterface();
		}

		private void InitUserInterface()
		{
			switch (DataSourceDropDownList.SelectedValue)
			{
					// Xml datasource
				case "2" :
				{
					FullOptionPlaceholder.Visible = false;
					XmlFileNameTextbox.Visible = true;
					XmlFileNameLabel.Visible = true;
					FieldOptionsPlaceHolder.Visible = false;
					MandatorySelectionCheckBox.Visible = true;
					SelectionRequiredLabel.Visible = true;
					SqlQueryLabel.Visible = false;
					SqlQueryTextbox.Visible = false;
					SqlQueryInfoLabel.Visible = false;
					break;
				}

					// Sql datasource
				case "3" :
				{
					FullOptionPlaceholder.Visible = false;
					XmlFileNameTextbox.Visible = false;
					XmlFileNameLabel.Visible = false;
					FieldOptionsPlaceHolder.Visible = false;
					MandatorySelectionCheckBox.Visible = true;
					SelectionRequiredLabel.Visible = true;
					SqlQueryLabel.Visible = true;
					SqlQueryTextbox.Visible = true;
					SqlQueryInfoLabel.Visible = true;

					break;
				}
					// none
				default :
				{
					FullOptionPlaceholder.Visible = true;
					XmlFileNameTextbox.Visible = false;
					XmlFileNameLabel.Visible = false;
					FieldOptionsPlaceHolder.Visible = FieldTypeCheckBox.Checked;
					MandatorySelectionCheckBox.Visible = false;
					SelectionRequiredLabel.Visible = false;
					SqlQueryLabel.Visible = false;
					SqlQueryTextbox.Visible = false;
					SqlQueryInfoLabel.Visible = false;
					break;
				}
			}
		}

		private void CreateTypeButton_Click(object sender, System.EventArgs e)
		{
			AnswerTypeData answerTypeData = null;
			if (TitleTextBox.Text.Length == 0)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingAnswerTypeTitleMessage");
			}
			else if (!SelectionTypeCheckBox.Checked && DataSourceDropDownList.SelectedValue == "1" && !FieldTypeCheckBox.Checked)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingTypeMessage");
			}
			else
			{
				if (DataSourceDropDownList.SelectedValue == "2")
				{
					answerTypeData = CreateXmlTypeFromForm();
				}
				else if (DataSourceDropDownList.SelectedValue == "3" &&  
					GlobalConfig.SqlBasedAnswerTypesAllowed &&
					CheckRight(NSurveyRights.SqlAnswerTypesEdition, false))
				{
					answerTypeData = CreateSqlTypeFromForm();
				}
				else if (DataSourceDropDownList.SelectedValue == "1")
				{
					answerTypeData = CreateTypeFromForm();
				}
			}

			if (answerTypeData != null)
			{
				new AnswerType().AddAnswerType(answerTypeData, NSurveyUser.Identity.UserId);
				UINavigator.NavigateToTypeEditor(MenuIndex);
			}
		}


		private AnswerTypeData CreateTypeFromForm()
		{
			int TypeMode = 0;
			AnswerTypeData answerTypeData = new AnswerTypeData();
			AnswerTypeData.AnswerTypesRow newAnswerType = answerTypeData.AnswerTypes.NewAnswerTypesRow();
		
			newAnswerType.Description = TitleTextBox.Text;
			if (SelectionTypeCheckBox.Checked)
			{
				TypeMode += (int)AnswerTypeMode.Selection;
				newAnswerType.TypeAssembly = "Votations.NSurvey.WebControls";
				newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerSelectionItem";
			}

			if (FieldTypeCheckBox.Checked )
			{
				if (!ValidateFieldOptions())
				{
					return null;
				}

				TypeMode += (int)AnswerTypeMode.Field;
				if (SelectionTypeCheckBox.Checked)
				{
					newAnswerType.TypeAssembly = "Votations.NSurvey.WebControls";
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerOtherFieldItem";
				}
				else
				{
					newAnswerType.TypeAssembly = "Votations.NSurvey.WebControls";
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerFieldItem";
				}
			}

			newAnswerType.TypeMode = TypeMode;
			newAnswerType.FieldHeight = int.Parse(FieldHeightTextBox.Text);
			newAnswerType.FieldWidth = int.Parse(FieldWidthTextBox.Text);
			newAnswerType.FieldLength = int.Parse(FieldMaxLengthTextBox.Text);
			newAnswerType.PublicFieldResults = FieldShownInResultsCheckBox.Checked;
			newAnswerType.JavascriptFunctionName = JavascriptFunctionNameTextBox.Text;
			newAnswerType.JavascriptErrorMessage = JavascriptErrorMessageTextBox.Text;
			newAnswerType.JavascriptCode = JavascriptTextBox.Text;	
			answerTypeData.AnswerTypes.AddAnswerTypesRow(newAnswerType);
			return answerTypeData;

		}

		private bool ValidateFieldOptions()
		{
			if (!Information.IsNumeric(FieldHeightTextBox.Text))
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("InvalidFieldHeightMessage");
				return false;
			}
				
			if (!Information.IsNumeric(FieldMaxLengthTextBox.Text))
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("InvalidFieldLengthMessage");
				return false;
			}

			if (!Information.IsNumeric(FieldWidthTextBox.Text))
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("InvalidFieldWidthMessage");
				return false;
			}

			return true;
		}
		

		private AnswerTypeData CreateXmlTypeFromForm()
		{
			AnswerTypeData answerTypeData = null;
			if (XmlFileNameTextbox.Text.Length == 0)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingXmlDataSourceMessage");
			}
			else
			{
				answerTypeData = new AnswerTypeData();
				AnswerTypeData.AnswerTypesRow newAnswerType = answerTypeData.AnswerTypes.NewAnswerTypesRow();
				newAnswerType.Description = TitleTextBox.Text;
				newAnswerType.TypeMode = (int)AnswerTypeMode.DataSource;
				newAnswerType.XmlDataSource = XmlFileNameTextbox.Text;
				newAnswerType.TypeAssembly = "Votations.NSurvey.WebControls";
				if (MandatorySelectionCheckBox.Checked)
				{
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerXmlRequiredListItem";
				}
				else
				{
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerXmlListItem";
				}
				Trace.Warn(newAnswerType.TypeNameSpace);
				answerTypeData.AnswerTypes.AddAnswerTypesRow(newAnswerType);
			}

			return answerTypeData;
		}

		private AnswerTypeData CreateSqlTypeFromForm()
		{
			AnswerTypeData answerTypeData = null;
			if (SqlQueryTextbox.Text.Length == 0)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingQueryDataSourceMessage");
			}
			else if (SqlQueryTextbox.Text.IndexOf("update") > -1  ||
				SqlQueryTextbox.Text.IndexOf("delete") > -1 || SqlQueryTextbox.Text.IndexOf("create") > -1 ||
				SqlQueryTextbox.Text.IndexOf("truncate") > -1 || SqlQueryTextbox.Text.IndexOf("fetch") > -1 || 
				SqlQueryTextbox.Text.IndexOf("grant") > -1 || SqlQueryTextbox.Text.IndexOf("insert") > -1 || 
				SqlQueryTextbox.Text.IndexOf("revoke") > -1 || SqlQueryTextbox.Text.IndexOf("open") > -1 || 
				SqlQueryTextbox.Text.IndexOf("alter") > -1) 
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("OnlySelectQueryAllowedMessage");
			}
			else
			{
				answerTypeData = new AnswerTypeData();
				AnswerTypeData.AnswerTypesRow newAnswerType = answerTypeData.AnswerTypes.NewAnswerTypesRow();
				newAnswerType.Description = TitleTextBox.Text;
				newAnswerType.TypeMode = (int)AnswerTypeMode.DataSource;
				SqlQueryTextbox.Text.Replace("delete ","");
				SqlQueryTextbox.Text.Replace("update ","");
				SqlQueryTextbox.Text.Replace("truncate ","");

				newAnswerType.DataSource= SqlQueryTextbox.Text;
				newAnswerType.TypeAssembly = "Votations.NSurvey.WebControls";
				if (MandatorySelectionCheckBox.Checked)
				{
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerSqlRequiredListItem";
				}
				else
				{
					newAnswerType.TypeNameSpace = "Votations.NSurvey.WebControls.UI.AnswerSqlListItem";
				}
				answerTypeData.AnswerTypes.AddAnswerTypesRow(newAnswerType);
			}

			return answerTypeData;
		}

		protected void OnOptionChanged()
		{
			if (OptionChanged != null)
			{
				OptionChanged(this, EventArgs.Empty);
			}
		}

		private void ApplyChangesButton_Click(object sender, System.EventArgs e)
		{
			AnswerTypeData answerTypeData = null;
			if (TitleTextBox.Text.Length == 0)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingAnswerTypeTitleMessage");
			}
			else if (!SelectionTypeCheckBox.Checked && DataSourceDropDownList.SelectedValue == "1" && 
				!FieldTypeCheckBox.Checked)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("MissingTypeMessage");
			}
			else
			{
				if (DataSourceDropDownList.SelectedValue == "2")
				{
					answerTypeData = CreateXmlTypeFromForm();
				}
				else if ( DataSourceDropDownList.SelectedValue == "3" &&
					GlobalConfig.SqlBasedAnswerTypesAllowed &&
					CheckRight(NSurveyRights.SqlAnswerTypesEdition, false))
				{
					answerTypeData = CreateSqlTypeFromForm();
				}
				else if (DataSourceDropDownList.SelectedValue == "1")
				{
					answerTypeData = CreateTypeFromForm();
				}
			}

			if (answerTypeData != null)
			{
				answerTypeData.AnswerTypes[0].AnswerTypeId = AnswerTypeId;
				new AnswerType().UpdateAnswerType(answerTypeData);
				OnOptionChanged();
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("AnswerTypeUpdatedMessage");
			}

			InitUserInterface();
		}

		private void DeleteTypeButton_Click(object sender, System.EventArgs e)
		{
			try 
			{
				new AnswerType().DeleteAnswerType(AnswerTypeId);
				Visible = false;
				OnOptionChanged();
			}
			catch (AnswerTypeInUseException)
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("AnswerTypeInUse");
			}
			InitUserInterface();

		}

		private void MakeBuiltInButton_Click(object sender, System.EventArgs e)
		{
			new AnswerType().SetBuiltInAnswerType(AnswerTypeId);
			OnOptionChanged();
			InitUserInterface();
		}

		private void DataSourceDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (DataSourceDropDownList.SelectedValue == "3" && 
				(!CheckRight(NSurveyRights.SqlAnswerTypesEdition, false) ||
				!GlobalConfig.SqlBasedAnswerTypesAllowed))
			{
				MessageLabel.Visible = true;
				MessageLabel.Text = GetPageResource("SqlAnswerTypeAccessDeniedMessage");
				DataSourceDropDownList.ClearSelection();
				if (AnswerTypeId != -1)
				{
					BindFields();
				}
			}
			
			InitUserInterface();
		}
	}
}
